﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Rsdn.SmartApp;

namespace Rsdn.Janus
{
    /// <summary>
    /// Собирает сообщения в течении периода времени и отображает их
    /// </summary>
    internal class DelayedMessage
    {
        const int MessagesDelay = 1000;
        private const int MaxMessages = 20;
        private readonly IServiceProvider _provider;
        private string mainText;
        private System.Timers.Timer timer = new System.Timers.Timer();

        private IWin32Window mainWindowParentInternal;
        private string msgInternal;
        List<string> messages = new List<string>();

        public DelayedMessage(IServiceProvider provider, string mainText)
        {
            _provider = provider;
            this.mainText = mainText;
            timer.Interval = MessagesDelay;
            timer.Elapsed += timer_Elapsed;
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            string messageText;
            timer.Stop();
            lock (messages)
            {
                messageText = GetMessageText();
                messages.Clear();
            }

            if (!string.IsNullOrEmpty(messageText))
            {
                NotifyAboutError(messageText);
            }
        }

        private void ShowErrorInternal()
        {
            MessageBox.Show(
                mainWindowParentInternal,
                msgInternal,
                SR.Forum.GoToMessage.ValidationError,
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
        }

        public void AddErrorChildLine(string text)
        {
            lock(messages)
            {
                messages.Add(text);
            }

            int count=0;
            lock (messages)
            {
                count = messages.Count;
            }

            if (count > MaxMessages)
            {
                string messageText = GetMessageText();
                lock (messages)
                {
                    messages.Clear();
                }

                NotifyAboutError(messageText);
            }
            else
            {
                timer.Start();
            }
        }

        private string GetMessageText()
        {
            string summarryText = mainText;
            summarryText += "\r\n";
            foreach (string message in messages)
            {
                summarryText += message;
                summarryText += "\r\n";
            }
            return summarryText;
        }

        private void NotifyAboutError(string msg)
        {
            IUIShell uiShell = _provider.GetRequiredService<IUIShell>();
            IWin32Window mainWindowParent = uiShell.GetMainWindowParent();
            AsyncOperation asyncOperation = uiShell.CreateUIAsyncOperation();
            mainWindowParentInternal = mainWindowParent;
            msgInternal = msg;
            asyncOperation.Post(ShowErrorInternal);

            //Control control = Control.FromHandle(mainWindowParent.Handle);
            //if (control.InvokeRequired)
            //{
            //    MethodInvoker method = delegate { NotifyAboutError(msg); };
            //    control.Invoke(method);
            //}
            //else
            //{
            //    ShowErrorInternal(mainWindowParent, msg);
            //}
        }

    }
}